<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="产品sku" prop="sku">
        <el-input v-model="queryParams.sku" placeholder="产品sku" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="用户登录名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="用户昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="外部sku" prop="sellerSku">
        <el-input v-model="queryParams.sellerSku" placeholder="外部sku" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="中文名称" prop="nameCn">
        <el-input v-model="queryParams.nameCn" placeholder="中文名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="英文名称" prop="nameEn">
        <el-input v-model="queryParams.nameEn" placeholder="英文名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单价" prop="pric">
        <el-input v-model="queryParams.pric" placeholder="单价" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-select v-model="queryParams.unit" placeholder="请选择单位" clearable>
          <el-option v-for="dict in dict.type.sys_product_unit" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="海关编码" prop="customsCode">
        <el-input v-model="queryParams.customsCode" placeholder="海关编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="申报价值" prop="declaredPric">
        <el-input v-model="queryParams.declaredPric" placeholder="申报价值" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="原产地" prop="originAddress">
        <el-input v-model="queryParams.originAddress" placeholder="原产地" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品规格" prop="producSpecifications">
        <el-input v-model="queryParams.producSpecifications" placeholder="产品规格" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="重量" prop="weight">
        <el-input v-model="queryParams.weight" placeholder="重量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="长度" prop="length">
        <el-input v-model="queryParams.length" placeholder="长度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="宽度" prop="width">
        <el-input v-model="queryParams.width" placeholder="宽度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="高度" prop="height">
        <el-input v-model="queryParams.height" placeholder="高度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="体积" prop="volume">
        <el-input v-model="queryParams.volume" placeholder="体积" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="颜色" prop="color">
        <el-input v-model="queryParams.color" placeholder="颜色" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="包含电池" prop="battery">
        <el-select v-model="queryParams.battery" placeholder="请选择包含电池" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品类型" prop="goodsType">
        <el-select v-model="queryParams.goodsType" placeholder="请选择产品类型" clearable>
          <el-option v-for="dict in dict.type.sys_product_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="出库策略" prop="goStrategy">
        <el-select v-model="queryParams.goStrategy" placeholder="请选择出库策略" clearable>
          <el-option v-for="dict in dict.type.sys_go_strategy" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="批次开关" prop="batchPoewr">
        <el-select v-model="queryParams.batchPoewr" placeholder="请选择批次开关" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="质保开关" prop="expirationDatePower">
        <el-select v-model="queryParams.expirationDatePower" placeholder="请选择保质期开关" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="质保天数" prop="expirationDateDay">
        <el-input v-model="queryParams.expirationDateDay" placeholder="保质期天数" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="质保预警" prop="expirationDateWarning">
        <el-input v-model="queryParams.expirationDateWarning" placeholder="保质期预警天数" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="库存预警" prop="inventoryPower">
        <el-select v-model="queryParams.inventoryPower" placeholder="库存预警" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="库存上限" prop="inventoryMax">
        <el-input v-model="queryParams.inventoryMax" placeholder="预警上限" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="库存下限" prop="inventoryMin">
        <el-input v-model="queryParams.inventoryMin" placeholder="预警下限" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="允许分销" prop="consignment">
        <el-select v-model="queryParams.consignment" placeholder="是否允许分销" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="分销截止" prop="consignmentEndTime">
        <el-date-picker clearable v-model="queryParams.consignmentEndTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择分销截止时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="分销售价" prop="consignmentMinRetaiPrice">
        <el-input v-model="queryParams.consignmentMinRetaiPrice" placeholder="分销零售价" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="u-m-b-10 u-m-t-10">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['wms:product:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['wms:product:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['wms:product:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['wms:product:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
          v-hasPermi="['wms:product:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="ckg2" plain icon="el-icon-delete" size="mini" @click="cleadRedis"
          v-hasPermi="['wms:product:cleadRedis']">清空缓存</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" sortable />
      <el-table-column label="归属用户" align="center" prop="avatarUrl" width="180">
        <template slot-scope="scope">
          <div class="u-flex">
            <div>
              <image-preview :src="scope.row.avatarUrl" :width="50" :height="50" />
            </div>
            <div class="u-m-l-10">
              <div class="nickname">{{ scope.row.userName }}</div>
              <div>{{ scope.row.nickName }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="产品sku" align="center" prop="sku" width="200" sortable />
      <el-table-column label="产品名称" align="center" width="200" sortable>
        <template slot-scope="scope">
          <div>{{ scope.row.nameCn }}</div>
          <div class="nickname">{{ scope.row.nameEn }}</div>
        </template>
      </el-table-column>
      <el-table-column label="产品图片" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="重量" align="center" prop="weight" sortable />
      <el-table-column label="长度" align="center" prop="length" sortable />
      <el-table-column label="宽度" align="center" prop="width" sortable />
      <el-table-column label="高度" align="center" prop="height" sortable />
      <el-table-column label="体积" align="center" prop="volume" sortable />
      <el-table-column label="颜色" align="center" prop="color" />
      <el-table-column label="外部sku" align="center" prop="sellerSku" />

      <el-table-column label="单价" align="center" prop="pric" sortable />
      <el-table-column label="单位" align="center" prop="unit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_product_unit" :value="scope.row.unit" />
        </template>
      </el-table-column>
      <el-table-column label="海关编码" align="center" prop="customsCode" />
      <el-table-column label="申报价值" align="center" prop="declaredPric" sortable width="100" />
      <el-table-column label="原产地" align="center" prop="originAddress" />
      <el-table-column label="产品规格" align="center" prop="producSpecifications" />


      <el-table-column label="包含电池" align="center" prop="battery" sortable width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.battery" />
        </template>
      </el-table-column>
      <el-table-column label="产品类型" align="center" prop="goodsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_product_type" :value="scope.row.goodsType" />
        </template>
      </el-table-column>
      <el-table-column label="产品备注" align="center" prop="description" />
      <el-table-column label="出库策略" align="center" prop="goStrategy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_go_strategy" :value="scope.row.goStrategy" />
        </template>
      </el-table-column>
      <el-table-column label="批次开关" align="center" prop="batchPoewr">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.batchPoewr" />
        </template>
      </el-table-column>
      <el-table-column label="质保开关" align="center" prop="expirationDatePower">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.expirationDatePower" />
        </template>
      </el-table-column>
      <el-table-column label="质保天数" align="center" prop="expirationDateDay" sortable width="100" />
      <el-table-column label="质保预警天数" align="center" prop="expirationDateWarning" sortable width="140" />
      <el-table-column label="库存预警" align="center" prop="inventoryPower">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.inventoryPower" />
        </template>
      </el-table-column>
      <el-table-column label="库存预警上限" align="center" prop="inventoryMax" sortable width="150" />
      <el-table-column label="库存预警下限" align="center" prop="inventoryMin" sortable width="150" />
      <!-- <el-table-column label="是否允许分销" align="center" prop="consignment">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.consignment" />
        </template>
      </el-table-column>
      <el-table-column label="分销截止时间" align="center" prop="consignmentEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.consignmentEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分销备注" align="center" prop="consignmentDescription" />
      <el-table-column label="分销零售价" align="center" prop="consignmentMinRetaiPrice" />
      <el-table-column label="分销的详情" align="center" prop="consignmentText" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:product:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['wms:product:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改产品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px" :inline="true">

        <el-form-item label="归属用户" prop="userName">
          <div class="u-flex">
            <div v-if="form.avatarUrl != null && form.avatarUrl != ''">
              <image-preview :src="form.avatarUrl" :width="80" :height="80" shape="circle" />
            </div>
            <div class="u-m-l-10">
              <div>
                {{ form.nickName }}
              </div>
              <div>
                <el-button plain type="primary" @click="userSelect" size="mini">选择用户</el-button>
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="产品sku" prop="sku">
          <div class="u-flex">
            <div>
              <el-input v-model="form.sku" placeholder="产品sku" style="width: 150px" />
            </div>
            <div>
              <el-button type="primary" size="mini" plain @click="randomNumber" v-if="form.id == null">生成</el-button>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="外部sku" prop="sellerSku">
          <el-input v-model="form.sellerSku" placeholder="外部sku" />
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品图片" prop="img">
              <image-upload v-model="form.img" :isShowTip="false" />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-row>
              <el-col :span="12">
                <el-form-item label="中文名称" prop="nameCn">
                  <el-input v-model="form.nameCn" placeholder="中文名称" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="英文名称" prop="nameEn">
                  <el-input v-model="form.nameEn" placeholder="英文名称" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单价" prop="pric">
                  <el-input v-model="form.pric" placeholder="单价" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单位" prop="unit">
                  <el-select v-model="form.unit" placeholder="请选择单位" style="width: 200px">
                    <el-option v-for="dict in dict.type.sys_product_unit" :key="dict.value" :label="dict.label"
                      :value="dict.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="包含电池" prop="battery">
                  <el-select v-model="form.battery" placeholder="请选择包含电池" style="width: 200px">
                    <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                      :value="dict.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="产品类型" prop="goodsType">
                  <el-select v-model="form.goodsType" placeholder="请选择产品类型" style="width: 200px">
                    <el-option v-for="dict in dict.type.sys_product_type" :key="dict.value" :label="dict.label"
                      :value="dict.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-form-item label="海关编码" prop="customsCode">
          <el-input v-model="form.customsCode" placeholder="海关编码" />
        </el-form-item>
        <el-form-item label="申报价值" prop="declaredPric">
          <el-input v-model="form.declaredPric" placeholder="申报价值" type="number" />
        </el-form-item>
        <el-form-item label="原产地" prop="originAddress">
          <el-input v-model="form.originAddress" placeholder="原产地" />
        </el-form-item>
        <el-form-item label="产品规格" prop="producSpecifications">
          <el-input v-model="form.producSpecifications" placeholder="产品规格" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="重量" type="number" />
        </el-form-item>
        <el-form-item label="长度" prop="length">
          <el-input v-model="form.length" placeholder="长度" type="number" />
        </el-form-item>
        <el-form-item label="宽度" prop="width">
          <el-input v-model="form.width" placeholder="宽度" type="number" />
        </el-form-item>
        <el-form-item label="高度" prop="height">
          <el-input v-model="form.height" placeholder="高度" type="number" />
        </el-form-item>
        <el-form-item label="体积" prop="volume">
          <el-input v-model="form.volume" placeholder="体积" type="number" />
        </el-form-item>
        <el-form-item label="颜色" prop="color">
          <el-input v-model="form.color" placeholder="颜色" />
        </el-form-item>
        <el-form-item label="出库策略" prop="goStrategy">
          <el-select v-model="form.goStrategy" placeholder="请选择出库策略" style="width: 200px">
            <el-option v-for="dict in dict.type.sys_go_strategy" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="批次开关" prop="batchPoewr">
          <el-select v-model="form.batchPoewr" placeholder="请选择批次开关" style="width: 200px">
            <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="质保开关" prop="expirationDatePower">
          <el-select v-model="form.expirationDatePower" placeholder="请选择保质期开关" style="width: 200px">
            <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <template v-if="form.expirationDatePower == 'Y'">
          <el-form-item label="质保天数" prop="expirationDateDay" :rules="[{ required: true, message: '质保天数不能为空' }]">
            <el-input v-model.number="form.expirationDateDay" placeholder="产品保质期天数" type="number" />
          </el-form-item>
          <el-form-item label="质保预警" prop="expirationDateWarning" :rules="[{ required: true, message: '质保期预警天数不能为空' }]">
            <el-input v-model.number="form.expirationDateWarning" placeholder="保质期预警天数" type="number" />
          </el-form-item>
        </template>

        <el-form-item label="库存预警" prop="inventoryPower">
          <el-select v-model="form.inventoryPower" placeholder="请选择库存预警" style="width: 200px">
            <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <template v-if="form.inventoryPower == 'Y'">
          <el-form-item label="库存上限" prop="inventoryMax" :rules="[{ required: true, message: '库存上限不能为空' }]">
            <el-input v-model.number="form.inventoryMax" placeholder="库存预警上限" type="number" />
          </el-form-item>
          <el-form-item label="库存下限" prop="inventoryMin" :rules="[{ required: true, message: '库存下限不能为空' }]">
            <el-input v-model.number="form.inventoryMin" placeholder="库存预警下限" type="number" />
          </el-form-item>
        </template>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="产品备注" prop="description">
              <el-input v-model="form.description" type="textarea" placeholder="内容" :rows="5" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="内容" :rows="5" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-form-item label="是否允许分销" prop="consignment">
          <el-select v-model="form.consignment" placeholder="请选择是否允许分销">
            <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item> 
          <el-form-item label="分销截止时间" prop="consignmentEndTime">
            <el-date-picker clearable v-model="form.consignmentEndTime" type="date" value-format="yyyy-MM-dd"
              placeholder="请选择分销截止时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="分销备注" prop="consignmentDescription">
            <el-input v-model="form.consignmentDescription" placeholder="分销备注" />
          </el-form-item>
          <el-form-item label="分销零售价" prop="consignmentMinRetaiPrice">
            <el-input v-model="form.consignmentMinRetaiPrice" placeholder="分销零售价" />
          </el-form-item>
          <el-form-item label="分销的详情">
            <editor v-model="form.consignmentText" :min-height="192" />
          </el-form-item> -->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择用户的弹窗 -->
    <el-dialog title="选择用户" :visible.sync="userSelectWindows" width="750px" :close-on-click-modal="false">
      <user-select v-if="userSelectWindows" @submit="userSelectSubmit"></user-select>
    </el-dialog>
    <!-- 选择用户弹窗结束-->

  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct, cleadRedis } from "@/api/wms/product";
import { getToken } from "@/utils/auth";
import userSelect from "../../system/user/profile/userSelect.vue";
export default {
  name: "Product",
  dicts: ['sys_go_strategy', 'sys_product_type', 'sys_product_unit', 'sys_yes_no'],
  components: { userSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品管理表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "id",
        isAsc: "desc",
        sku: null,
        userName: null,
        nickName: null,
        sellerSku: null,
        nameCn: null,
        nameEn: null,
        pric: null,
        unit: null,
        customsCode: null,
        declaredPric: null,
        originAddress: null,
        producSpecifications: null,
        weight: null,
        length: null,
        width: null,
        height: null,
        volume: null,
        color: null,
        battery: null,
        goodsType: null,
        description: null,
        goStrategy: null,
        batchPoewr: null,
        expirationDatePower: null,
        expirationDateDay: null,
        expirationDateWarning: null,
        inventoryPower: null,
        inventoryMax: null,
        inventoryMin: null,
        consignment: null,
        consignmentEndTime: null,
        consignmentDescription: null,
        consignmentMinRetaiPrice: null,
        consignmentText: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sku: [
          { required: true, message: "产品sku不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "归属用户不能为空", trigger: "blur" }
        ],
      },
      // 导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/wms/product/importData"
      },
      // 是否打开选择用户的窗口
      userSelectWindows: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 选择用户后的反射 */
    userSelectSubmit(e) {
      this.form.userName = e.userName
      this.form.nickName = e.nickName
      this.form.avatarUrl = e.avatar
      this.userSelectWindows = false
    },
    /** 选择用户被点击*/
    userSelect() {
      this.userSelectWindows = true
    },
    // 生成5位随机包裹号
    randomNumber() {
      this.form.sku = Math.random().toString().substring(2, 20)
    },
    /** 查询产品管理列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sku: null,
        userName: null,
        avatarUrl: null,
        nickName: null,
        sellerSku: null,
        nameCn: null,
        nameEn: null,
        img: null,
        pric: null,
        unit: null,
        customsCode: null,
        declaredPric: null,
        originAddress: null,
        producSpecifications: null,
        weight: null,
        length: null,
        width: null,
        height: null,
        volume: null,
        color: null,
        battery: null,
        goodsType: null,
        description: null,
        goStrategy: null,
        batchPoewr: null,
        expirationDatePower: null,
        expirationDateDay: null,
        expirationDateWarning: null,
        inventoryPower: null,
        inventoryMax: null,
        inventoryMin: null,
        consignment: null,
        consignmentEndTime: null,
        consignmentDescription: null,
        consignmentMinRetaiPrice: null,
        consignmentText: null,
        createBody: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品管理编号为"' + ids + '"的数据项？').then(function () {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('wms/product/importTemplate', {
      }, `template_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "产品管理导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 清空所有缓存 */
    cleadRedis() {
      cleadRedis().then(response => {
        this.$modal.msgSuccess("清空成功");
      });
    },
  }
};
</script>
<style scoped>
.el-form-item--small.el-form-item {
  margin-bottom: 1px !important;
}

.el-form--inline .el-form-item {
  width: 300px;
}

.nickname {
  color: #93979e;
  font-size: 12px;
}
</style>